package com.xueyi.workflow.service.impl;

import com.xueyi.workflow.service.IWorkflowInstanceService;
import com.xueyi.workflow.service.IWorkflowUserService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * 工作流用户服务实现类
 *
 * @author xueyi
 * @date 2024-07-03
 */
@Slf4j
@Service
public class WorkflowUserServiceImpl implements IWorkflowUserService {

    @Autowired
    private IWorkflowInstanceService workflowInstanceService;

    @Override
    public Set<Long> getUserIdsByRoleIds(Set<Long> roleIds) {
        log.info("根据角色ID查询用户: {}", roleIds);
        
        // TODO: 实际实现时应该调用系统服务
        // 例如：organizeService.selectUserSetByRoleIds(roleIds)
        
        // 模拟数据 - 只返回真实存在的用户ID
        Set<Long> userIds = new java.util.HashSet<>();
        for (Long roleId : roleIds) {
            if (roleId == 1L) {
                userIds.add(1L); // admin用户
            } else if (roleId == 2L) {
                userIds.add(1L); // admin用户
                userIds.add(1952534831703269376L); // lin用户
            }
        }
        
        log.info("角色对应的用户: {}", userIds);
        return userIds;
    }

    @Override
    public Set<Long> getUserIdsByDeptIds(Set<Long> deptIds) {
        log.info("根据部门ID查询用户: {}", deptIds);
        
        // TODO: 实际实现时应该调用系统服务
        // 例如：organizeService.selectUserSetByDeptIds(deptIds)
        
        // 模拟数据 - 只返回真实存在的用户ID
        Set<Long> userIds = new java.util.HashSet<>();
        for (Long deptId : deptIds) {
            if (deptId == 1L) {
                userIds.add(1L); // admin用户
            } else if (deptId == 99L) {
                userIds.add(1L); // admin用户
                userIds.add(1952534831703269376L); // lin用户
            }
        }
        
        log.info("部门对应的用户: {}", userIds);
        return userIds;
    }

    @Override
    public Set<Long> getUserIdsByPostIds(Set<Long> postIds) {
        log.info("根据岗位ID查询用户: {}", postIds);
        
        // TODO: 实际实现时应该调用系统服务
        // 例如：organizeService.selectUserSetByPostIds(postIds)
        
        // 模拟数据 - 只返回真实存在的用户ID
        Set<Long> userIds = new java.util.HashSet<>();
        for (Long postId : postIds) {
            if (postId == 1L) {
                userIds.add(1L); // admin用户
            } else if (postId == 2L) {
                userIds.add(1952534831703269376L); // lin用户
            }
        }
        
        log.info("岗位对应的用户: {}", userIds);
        return userIds;
    }

    @Override
    public Set<Long> getUserIdsByGroups(Set<Long> roleIds, Set<Long> deptIds, Set<Long> postIds) {
        log.info("根据角色、部门、岗位ID查询用户: roleIds={}, deptIds={}, postIds={}", roleIds, deptIds, postIds);
        
        Set<Long> allUserIds = new java.util.HashSet<>();
        
        // 根据角色查询用户
        if (!roleIds.isEmpty()) {
            Set<Long> roleUserIds = getUserIdsByRoleIds(roleIds);
            allUserIds.addAll(roleUserIds);
        }
        
        // 根据部门查询用户
        if (!deptIds.isEmpty()) {
            Set<Long> deptUserIds = getUserIdsByDeptIds(deptIds);
            allUserIds.addAll(deptUserIds);
        }
        
        // 根据岗位查询用户
        if (!postIds.isEmpty()) {
            Set<Long> postUserIds = getUserIdsByPostIds(postIds);
            allUserIds.addAll(postUserIds);
        }
        
        log.info("最终查询到的用户: {}", allUserIds);
        return allUserIds;
    }

    @Override
    public Set<Long> getUserIdsByExpressions(Set<String> expressions, DelegateTask delegateTask) {
        log.info("根据表达式查询用户: {}", expressions);
        
        Set<Long> userIds = new java.util.HashSet<>();
        
        for (String expression : expressions) {
            try {
                // 移除${}包装
                String expr = expression.replace("${", "").replace("}", "");
                
                switch (expr) {
                    case "starter":
                        // 流程发起人
                        Long starterId = workflowInstanceService.getStarterUserId(delegateTask);
                        if (starterId != null) {
                            userIds.add(starterId);
                        }
                        break;
                        
                    case "starterDeptLeader":
                        // 发起人部门负责人
                        Long deptId = workflowInstanceService.getStarterDeptId(delegateTask);
                        if (deptId != null) {
                            Set<Long> deptLeaderIds = workflowInstanceService.getDeptLeaderUserIds(deptId);
                            userIds.addAll(deptLeaderIds);
                        }
                        break;
                        
                    case "starterDeptParentLeader":
                        // 发起人上级部门负责人
                        Long parentDeptId = workflowInstanceService.getStarterParentDeptId(delegateTask);
                        if (parentDeptId != null) {
                            Set<Long> parentLeaderIds = workflowInstanceService.getDeptLeaderUserIds(parentDeptId);
                            userIds.addAll(parentLeaderIds);
                        }
                        break;
                        
                    default:
                        log.warn("未知的表达式: {}", expression);
                        break;
                }
            } catch (Exception e) {
                log.error("处理表达式失败: {}", expression, e);
            }
        }
        
        log.info("表达式对应的用户: {}", userIds);
        return userIds;
    }
} 